package cz.muni.fi.pa165.validate;

import java.util.ResourceBundle;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Named;

/**
 *
 * @author shifty
 */
@ApplicationScoped
@Named
public class CustomerValidator {
   private void checkString(Object string,String msg) throws ValidatorException{
        ResourceBundle res = ResourceBundle.getBundle("i18n.main", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        String val = (String)string;
        
        if(val == null || val.length() < 3) {
            FacesMessage message = new FacesMessage(res.getString(msg));
            throw new ValidatorException(message); 
        } 
    }
    
    public void validateName(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"error.form.empty.name");
    }
    public void validateAddress(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"validation.address.address");
    }
    public void validateZip(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"validation.address.zip");
    }
    public void validateCity(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"validation.address.city");
    }
    public void validateCountry(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"validation.address.country");
    }
    public void validateAccNumber(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        checkString(value,"validation.customer.accNumber");
    } 
}
